package com.example.ui.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author yliu
 */
@RestController
@Slf4j
public class UploadController {
    @GetMapping("/download")
    public ResponseEntity<Resource> downloadFile() {
        String fileName = "tts_res.tar.gz";
        String directory = "/home/jianhai/ychen/PaddleSpeech/examples/other/tts_finetune/tts3";
//        String directory = "D:\\";
        Path filePath = Paths.get(directory).resolve(fileName);
        log.info("开始下载");
        try {
            Resource resource = new UrlResource(filePath.toUri());
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return ResponseEntity.notFound().build();
        }
    }
}
